﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Wjtour.Common;
using Wjtour.Domain;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

namespace Wjtour.BLL
{
    public class sysadmin_bll : base_dll
    {
        public sysadmin login(string username, string pwd)
        {
            DbCommand cmd = DbAccess.CreateCommand();
            cmd.CommandText = "select * from sysadmin where adminname=@adminname and adminpass=@adminpass";
            cmd.CommandType = System.Data.CommandType.Text;
            DbAccess.AddInParameter(cmd, "@adminname", System.Data.DbType.String, username);
            DbAccess.AddInParameter(cmd, "@adminpass", System.Data.DbType.String, pwd);

            var user = DbAccess.GetObjects<sysadmin>(cmd).ToList().FirstOrDefault();
            return user;
        }

        public PaginationData<Wjtour.Domain.dto.sysadmin_role> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and dd.adminname like '%" + Utility.GetSafeSql(q) + "%' ";
            string sql = string.Format(@"select dd.*,wr.title rolename from sysadmin dd 
                inner join webrole wr on dd.role=wr.id
                order by dd.id desc", where);
            PaginationData<Wjtour.Domain.dto.sysadmin_role> data = GetPaginationData<Wjtour.Domain.dto.sysadmin_role>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.sysadmin Load(int id)
        {
            string sql = "select * from sysadmin where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.sysadmin>(sql).FirstOrDefault();
        }

        public bool Update(Wjtour.Domain.sysadmin entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE [sysadmin] SET 
                [adminname] = @adminname,
                [adminpass] = @adminpass,
                [lastlogintime] = @lastlogintime,
                [addtime] = @addtime,
                [lastloginip] = @lastloginip,
                [loginnum] = @loginnum,
                [logintime] = @logintime,
                [loginip] = @loginip,
                [role] = @role
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);
                DbAccess.AddInParameter(cmd, "@adminname", DbType.String, entity.adminname);
                DbAccess.AddInParameter(cmd, "@adminpass", DbType.String, entity.adminpass);
                DbAccess.AddInParameter(cmd, "@lastlogintime", DbType.DateTime, entity.lastlogintime);
                DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
                DbAccess.AddInParameter(cmd, "@lastloginip", DbType.String, entity.lastloginip);
                DbAccess.AddInParameter(cmd, "@loginnum", DbType.Int32, entity.loginnum);
                DbAccess.AddInParameter(cmd, "@logintime", DbType.DateTime, entity.logintime);
                DbAccess.AddInParameter(cmd, "@loginip", DbType.String, entity.loginip);
                DbAccess.AddInParameter(cmd, "@role", DbType.Int32, entity.role);
                cmd.CommandText = sql;
                cmd.CommandType = System.Data.CommandType.Text;
                return DbAccess.ExecuteNonQuery(cmd) > 0;
            }
            return false;
        }

        public bool Add(Wjtour.Domain.sysadmin sysadmin)
        {
            string sql = "";
            List<DbParameter> parameters = new List<DbParameter>();
            parameters.Add(new SqlParameter("@adminname", sysadmin.adminname));
            parameters.Add(new SqlParameter("@adminpass", sysadmin.adminpass));
            parameters.Add(new SqlParameter("@role", sysadmin.role));
            parameters.Add(new SqlParameter("@addtime", sysadmin.addtime));
            sql = string.Format(@"insert into sysadmin(adminname,adminpass,role,tag,addtime)
                    values(@adminname,@adminpass,@role,@addtime)");

            return DbAccess.ExecuteNonQuery(sql, parameters) > 0;
        }


        public int BatchDelete(string ids)
        {
            string sql = "delete from sysadmin where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
